package br.com.gbvbahia.mybooks.fragment;

import java.util.List;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import br.com.gbvbahia.commons.Transacao.Transacao;
import br.com.gbvbahia.mybooks.ActivityMae;
import br.com.gbvbahia.mybooks.R;
import br.com.gbvbahia.mybooks.adapters.LivroAdapter;
import br.com.gbvbahia.mybooks.dao.LivroDAO;
import br.com.gbvbahia.mybooks.modelos.LivroVO;

public class ListarLivrosFragment extends FragmentMae implements Transacao {

	private ListView listView;
	private List<LivroVO> livros;
	private String likeNomeLivro;
	private LivroDAO livroDAO;
	private EditText searchEdit;
	private ImageButton searchButton;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.listar, null);
		listView = (ListView) view.findViewById(R.id.listview);
		searchEdit = (EditText) view.findViewById(R.id.searchTextHeader);
		searchButton = (ImageButton) view.findViewById(R.id.btnSearch);
		livroDAO = new LivroDAO(getActivity().getBaseContext());
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int posicao, long id) {
				LivroVO c = (LivroVO) parent.getAdapter().getItem(posicao);
				FragmentTransfer ft = (FragmentTransfer) getActivity();
				ft.updateFragment(R.layout.listar, c);
			}
		});

		searchButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				pesquisa();
			}
		});

		searchEdit
				.setOnEditorActionListener(new TextView.OnEditorActionListener() {

					@Override
					public boolean onEditorAction(TextView v, int actionId,
							KeyEvent event) {
						if (actionId == EditorInfo.IME_ACTION_SEARCH
								|| actionId == 0) {
							pesquisa();
							return true;
						}
						return false;
					}
				});

		return view;
	}

	private void pesquisa() {
		if (!TextUtils.isEmpty(searchEdit.getText().toString())) {
			likeNomeLivro = searchEdit.getText().toString();
		} else {
			likeNomeLivro = "";
		}
		Log.d(ActivityMae.LOG_TAG, "BUSCABDO LIVRO OnKey: " + likeNomeLivro);
		startTransacao(ListarLivrosFragment.this);
	}

	@Override
	public void executar() throws Exception {
		if (TextUtils.isEmpty(likeNomeLivro)) {
			livros = livroDAO.getAll();
		} else {
			livros = livroDAO.getLivrosLikeName(likeNomeLivro);
		}
	}

	@Override
	public void atualizarView() {
		if (getActivity() != null && !getActivity().isFinishing())
			listView.setAdapter(new LivroAdapter(this, livros));
	}

	@Override
	public void onResume() {
		super.onResume();
		startTransacao(this);
	}

}
